package com.jiexin.service;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.jiexin.beans.LogType;
import com.jiexin.common.RequestHolder;
import com.jiexin.dao.SysLogMapper;
import com.jiexin.dao.SysRoleMapper;
import com.jiexin.dao.SysRoleUserMapper;
import com.jiexin.dao.SysUserMapper;
import com.jiexin.model.SysLogWithBLOBs;
import com.jiexin.model.SysRole;
import com.jiexin.model.SysRoleUser;
import com.jiexin.model.SysUser;
import com.jiexin.util.IpUtil;
import com.jiexin.util.JsonMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author: Jay_Liu
 * @Description:
 * @Date: Created in 17:59 2018/4/26 0026
 * @Modified By:
 */
@Service
public class SysRoleUserService {

    @Resource
    SysRoleUserMapper sysRoleUserMapper;

    @Resource
    SysUserMapper sysUserMapper;

    @Resource
    SysRoleMapper sysRoleMapper;

    @Resource
    private SysLogMapper sysLogMapper;

    public List<SysUser> getListByRoleId(Integer roleId) {
        List<Integer> userIdList = sysRoleUserMapper.getUserIdListByRoleId(roleId);
        if (CollectionUtils.isEmpty(userIdList)) {
            return Lists.newArrayList();
        }
        return sysUserMapper.getUserListByUserIdList(userIdList);
    }

    //保存角色与用户的关系
    public void changeRoleUsers(Integer roleId, List<Integer> userIdList) {
        //原先的角色下的用户ID集合
        List<Integer> originUserIdList = sysRoleUserMapper.getUserIdListByRoleId(roleId);
        //判断需要更新的用户集合是否与原先角色下的用户集合
        if (originUserIdList.size() == userIdList.size()) {
            Set<Integer> originUserIdSet = Sets.newHashSet(originUserIdList);
            Set<Integer> userIdSet = Sets.newHashSet(userIdList);
            originUserIdSet.removeAll(userIdSet);
            if (CollectionUtils.isEmpty(originUserIdSet)) {
                return;
            }
        }
        updateRoleUsers(roleId, userIdList);
        saveRoleUserLog(roleId, originUserIdList, userIdList);
    }

    @Transactional
    public void updateRoleUsers(Integer roleId, List<Integer> userIdList) {
        //通过角色ID删除用户与角色的对应关系
        sysRoleUserMapper.deleteByRoleId(roleId);
        if (CollectionUtils.isEmpty(userIdList)) {
            return;
        }
        //构建对象
        List<SysRoleUser> sysRoleUserList = Lists.newArrayList();
        for (Integer userId : userIdList) {
            SysRoleUser sysRoleUser = SysRoleUser.builder().roleId(roleId).userId(userId).operator(RequestHolder.getCurrentUser().getUsername())
                    .operateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest())).operateTime(new Date()).build();
            sysRoleUserList.add(sysRoleUser);
        }
        //更新新的用户与角色对应关系
        sysRoleUserMapper.batchInsert(sysRoleUserList);
    }

    public List<SysRole> getRoleListByUserId(Integer userId) {
        List<Integer> roleIdList = sysRoleUserMapper.getUserIdListByRoleIdList(Lists.newArrayList(userId));
        if(CollectionUtils.isEmpty(roleIdList)){
            return Lists.newArrayList();
        }
        return sysRoleMapper.getRoleListByIdList(roleIdList);
    }

    private void saveRoleUserLog(int roleId, List<Integer> before, List<Integer> after) {
        SysLogWithBLOBs log = new SysLogWithBLOBs();
        log.setType(LogType.TYPE_ROLE_USER);
        log.setTargetId(roleId);
        log.setOldValue(before == null ? "" : JsonMapper.obj2String(before));
        log.setNewValue(after == null ? "" : JsonMapper.obj2String(after));
        log.setOperator(RequestHolder.getCurrentUser().getUsername());
        log.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()));
        log.setOperateTime(new Date());
        log.setStatus(1);
        sysLogMapper.insertSelective(log);
    }

}